package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/1/15 10:10
 * @apiNote
 */

public class Test00038CountAndSay {
    public static void main(String[] args) {
        int n = 1;
        System.out.println(countAndSay(n));
    }
    public static String countAndSay(int n) {
        String str = "1";
        for (int i = 2; i <= n; ++i) {
            StringBuilder sb = new StringBuilder();
            int start = 0;
            int pos = 0;

            while (pos < str.length()) {
                while (pos < str.length() && str.charAt(pos) == str.charAt(start)) {
                    pos++;
                }
                sb.append(pos - start).append(str.charAt(start));
                start = pos;
            }
            str = sb.toString();
        }

        return str;
    }
}
